package top.llove.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.llove.mapper.ArticleMapper;
import top.llove.pojo.dto.ArticleAdminDto;
import top.llove.service.ArticleService;
import top.llove.utils.res.ApiResult;

import java.util.List;

/**
 * 文章Service实现类
 *
 * @author: 幸会
 * @date: 2021/6/17 17:52
 * @email: 1846724873@qq.com
 * @description: 文章Service实现类
 * version: 1.0
 */
@Slf4j
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper article;

    @Autowired
    private ApiResult res;


    @Override
    public ApiResult select(String id, String title, String kind, String user) {
        try {
            List<ArticleAdminDto> select = article.select(id, title, kind, user);
            Object data = select.size() == 0 ? "暂无数据" : select;
            log.info("查询文章成功: " + select);
            res = ApiResult.success(200, "查询成功", data);
        } catch (Exception e) {
            e.printStackTrace();
            res = ApiResult.fail(500, e.getMessage(), null);
        }
        return res;
    }

    @Override
    public ApiResult save(String title, String img, String desc, String context, String kind, String user) {
        boolean ok = false;
        try {
            ok = article.save(title, img, desc, context, kind, user) == 1;
            res = ApiResult.success(200, "添加" + (ok ? "成功" : "失败"), ok);
        } catch (Exception e) {
            e.printStackTrace();
            ok = false;
            res = ApiResult.fail(500, e.getMessage(), ok);
        }
        return res;
    }

    @Override
    public ApiResult edit(String title, String img, String desc, String context, String kind, String id) {
        boolean ok = false;
        try {
            ok = article.edit(title, img, desc, context, kind, id) == 1;
            res = ApiResult.success(200, "修改" + (ok ? "成功" : "失败"), ok);
        } catch (Exception e) {
            e.printStackTrace();
            ok = false;
            res = ApiResult.fail(500, e.getMessage(), ok);
        }
        return res;
    }

    @Override
    public ApiResult delete(String id, String user, String kind) {
        boolean ok = false;
        try {
            Integer row = article.delete(id, user, kind);
            ok = row >= 1;
            res = ApiResult.success(200, "删除" + (ok ? "成功" : "失败"), ok);
        } catch (Exception e) {
            e.printStackTrace();
            ok = false;
            res = ApiResult.fail(500, "删除失败", e.getMessage());
        }
        return res;
    }
}
